package tq.com.tqcom.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import tq.com.tqcom.R;

public class TQProcessBar extends FrameLayout {
    private TextView mProcess;
    private ProgressBar mProgressBar;
    private int type;

    public TQProcessBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        iniUi(attrs);
    }

    protected void iniUi(AttributeSet pAttributeSet){
        LayoutInflater.from(getContext()).inflate(R.layout.view_processbar_layout, this, true);

        TypedArray lTypedArray = getContext().obtainStyledAttributes(pAttributeSet, R.styleable.TQProcessBar);
        type = lTypedArray.getInt(R.styleable.TQProcessBar_TQProcessBar_type, 0);
        lTypedArray.recycle();
        mProcess = findViewById(R.id.title);
        mProgressBar = findViewById(R.id.progressBar);
    }

    public void setType(int t){
        type = 0;
    }
    public void updateProcess(int current){
        int tmp = current;
        tmp = tmp % 100;
        String prex = type == 0 ? "正在下载" : "正在上传";
        mProcess.setText(String.format("%s%d%%", prex, tmp));
        mProgressBar.setProgress(tmp);
    }

    public void setText(String txt){
        mProcess.setText(txt);
    }
}
